package Service

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/grpc"
	"selfService/tool"
	"selfService/兴趣相关/service/interest"
	"selfService/兴趣相关/service/user_inter_mid"
)

type InterestService struct {

}

var interestAddr = tool.GetConf().InterestAddr

// ** 已暴露
// rpc 调用兴趣相关 GetProdStock 服务  返回所有的兴趣选项
//rpc GetAllInterest (google.protobuf.Empty) returns (Interestlist);  // 获得所有兴趣选项（管理员）
func (this *InterestService) GetProdStockClient()(*interest.Interestlist, error) {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return nil,err
	}
	InterestClient := interest.NewInterestServiceClient(coon)
	result,err := InterestClient.GetAllInterest(context.Background(),&empty.Empty{})
	if err!=nil {
		return nil,err
	}
	return result,nil
}

// ** 已暴露
// rpc 调用兴趣相关 InsertInterest 服务  添加兴趣
//rpc InsertInterest(Interest) returns(InterestReponse);              // 添加兴趣（管理员）
func (this *InterestService)InsertInterest(i *interest.Interest)(string ,error)  {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return "失败",err
	}
	InterestClient := interest.NewInterestServiceClient(coon)
	result,err := InterestClient.InsertInterest(context.Background(),i)
	if err!=nil {
		return "失败",err
	}
	return result.Result,nil
}

// ** 已暴露
// rpc 调用兴趣相关 ChangeInterest 服务  根据id修改兴趣
//rpc ChangeInterest(InterestRequest) returns(InterestReponse);       // 根据id 修改兴趣选项（管理员端）
func (this *InterestService) ChangeInterest(i *interest.InterestRequest)(string ,error) {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return "失败",err
	}
	InterestClient := interest.NewInterestServiceClient(coon)
	result,err := InterestClient.ChangeInterest(context.Background(),i)
	if err!=nil {
		return "失败",err
	}
	return result.Result,nil
}


// ** 已暴露
//rpc 调用兴趣相关 DeleteInterestById 服务 删除兴趣
//rpc DeleteInterestById(UserInterMidRquest)returns(ChangeInterestResponse);//根据id  删除某个兴趣（管理员）
func (this *InterestService) DeleteInterestById(i *interest.UserInterMidRquest)(string ,error) {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return "失败",err
	}
	InterestClient := interest.NewInterestServiceClient(coon)
	result,err := InterestClient.DeleteInterestById(context.Background(),i)
	if err!=nil {
		return "失败",err
	}
	return result.State,nil
}

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————



// **  已暴露
// rpc 调用兴趣和个人结合中的  根据id 返回某人兴趣列表
//rpc GetInterestSelf (UserInterMidRquest) returns (User_Inter_midList);  // 获得个人兴趣列表
func (this *InterestService)GetMyselfInterest(id  string)(list *user_inter_mid.User_InterMidList,err error)  {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return nil,err
	}
	InterestClient := user_inter_mid.NewInterestServiceClient(coon)
	result,err := InterestClient.GetInterestSelf(context.Background(),&user_inter_mid.UserInterMidRquest{
		Id: id,
	})
	if err!=nil {
		return nil,err
	}
	return result,nil
}

// ** 已暴露
// rpc 调用兴趣和个人结合中  修改id  修改个人兴趣选项
//rpc ChangeSelfInterestById(ChangeInterestRequest) returns(ChangeInterestResponse); //根据id 修改个人兴趣选项
func (this *InterestService)ChangeSelfInterest(id string,interId string)string  {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return "调用rpc服务失败"
	}
	InterestClient := user_inter_mid.NewInterestServiceClient(coon)
	result,err := InterestClient.ChangeSelfInterestById(context.Background(),&user_inter_mid.ChangeInterestRequest{
		Id: id,
		Changeid: interId,
	})

	return result.State
}

//
// rpc 调用删除 兴趣和个人结合中  自己的某个兴趣选项
//rpc DeleteSelfInterestById(UserInterMidRquest) returns(ChangeInterestResponse); // 根据id 删除自己的某个兴趣选项
func (this *InterestService)DeleteSelfInterestById(id1 string)string  {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return "调用rpc服务失败"
	}
	InterestClient := user_inter_mid.NewInterestServiceClient(coon)
	result,err := InterestClient.DeleteSelfInterestById(context.Background(),&user_inter_mid.UserInterMidRquest{
		Id: id1,
	})

	return result.State
}


// 添加兴趣
//rpc InsertSelfInterest(Interest) returns(InterestReponse);
func (this *InterestService)InsertSelfInterest(userId string, interId string)string  {
	coon,err := grpc.Dial(interestAddr,grpc.WithInsecure())
	if err!=nil {
		return "调用rpc服务失败"
	}
	InterestClient := user_inter_mid.NewInterestServiceClient(coon)
	result,err := InterestClient.InsertSelfInterest(context.Background(),&user_inter_mid.Interest{
		UserId: userId,
		InterestId: interId,
	})

	return result.Result
}